﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

#if UNITY_EDITOR

namespace HXu.UnityEditor
{
    public class CreateFiles : EditorWindow
    {
        // 项目名称，用于创建根文件夹
        protected string ProjectName = "PROJECT_NAME";

        // 是否使用自定义文件夹结构
        protected bool UseMySelf = true;

        // Assets 文件夹下的默认子文件夹列表
        protected List<string> AssetsFolders = new List<string>()
        { "_HXu", "Fonts", "Editor", "Library", "Plugins", "StreamingAssets", "Resources" };

        // 自定义文件夹结构中的子文件夹列表
        protected List<string> HXuFolders = new List<string>()
        {"Animations", "Scenes", "Images", "Sprites", "Audios", "Videos", "Scripts", "Materials", "Prefabs", "Textures" };

        // Plugins 文件夹下的子文件夹列表
        protected List<string> PluginFolders = new List<string>()
        { "Android", "iOS", "Special" };

        // 项目根文件夹下的子文件夹列表
        protected List<string> ProjectFolders = new List<string>()
        { "Animator", "Art", "Code", "Level", "Media", "Resources", "Sequences", "Settings" };

        // Animator 文件夹下的子文件夹列表
        protected List<string> AnimatorFolders = new List<string>()
        { "Animals", "NPC", "Player", "UI", "Vehicles", "Weapons" };

        // Code 文件夹下的子文件夹列表
        protected List<string> CodeFolders = new List<string>()
        { "Editor", "Scripts", "Shaders" };

        // Level 文件夹下的子文件夹列表
        protected List<string> LevelFolders = new List<string>()
        { "Prefabs", "Scenes" };

        // Art 文件夹下的子文件夹列表
        protected List<string> ArtFolders = new List<string>()
        { "Materials", "Meshes", "Particles", "PostProcess", "RenderTextures", "Textures", "UI" };

        // UI 文件夹下的子文件夹列表
        protected List<string> UIFolders = new List<string>()
        { "Fonts", "Icons", "Sprites" };

        // Textures 文件夹下的子文件夹列表
        protected List<string> TextureFolders = new List<string>()
        { "General", "HDR", "VPX" };

        // Materials 文件夹下的子文件夹列表
        protected List<string> MaterialFolders = new List<string>()
        { "Background", "General", "Others", "Sky", "Water" };

        // Meshes 文件夹下的子文件夹列表
        protected List<string> MeshFolders = new List<string>()
        { "Animals", "Buildings", "Family", "Foliage", "NPC", "Offices", "Others", "Player", "Props", "Vehicles", "Vapors" };

        // Media 文件夹下的子文件夹列表
        protected List<string> MediaFolders = new List<string>()
        { "Audio", "Video" };

        // Audio 文件夹下的子文件夹列表
        protected List<string> AudioFolders = new List<string>()
        { "Music", "Sound" };

        // 菜单项：打开创建默认文件夹的窗口
        [MenuItem("EditorTools(HXu)/2.Create Default Folders", false, 2)]
        private static void CreateWindow()
        {
            Rect rect = new Rect(0, 0, 500, 500);
            CreateFiles window = GetWindowWithRect<CreateFiles>(rect, false, "Create Default Folders");
            window.Show();
        }

        // 菜单项：在 Assets 菜单中创建默认文件夹
        [MenuItem("Assets/Create Default Folders")]
        private static void SetUpFolders()
        {
            Rect rect = new Rect(0, 0, 500, 500);
            CreateFiles window = GetWindowWithRect<CreateFiles>(rect, false, "Create Default Folders");
            window.Show();
        }

        /// <summary>
        /// 创建指定路径下的文件夹列表
        /// </summary>
        /// <param name="folders">文件夹名称列表</param>
        /// <param name="path">父文件夹路径</param>
        protected void CreateFolders(List<string> folders, string path)
        {
            foreach (string folder in folders)
            {
                string fullPath = Path.Combine(path, folder);
                if (!Directory.Exists(fullPath))
                {
                    try
                    {
                        Directory.CreateDirectory(fullPath);
                        Debug.Log($"Created folder: {fullPath}");
                    }
                    catch (System.Exception e)
                    {
                        Debug.LogError($"Failed to create folder: {fullPath}. Error: {e.Message}");
                    }
                }
            }
        }

        /// <summary>
        /// 创建所有默认文件夹
        /// </summary>
        protected void CreateAllFolders()
        {
            string assetsPath = "Assets/";
            CreateFolders(AssetsFolders, assetsPath); // 创建 Assets 下的默认文件夹

            if (UseMySelf)
            {
                Debug.Log("Creating folders using myself structure.");
                string hXuPath = Path.Combine(assetsPath, "_HXu/");
                CreateFolders(HXuFolders, hXuPath); // 创建自定义文件夹结构
            }
            else
            {
                Debug.Log("Creating folders using standard structure.");

                //plugins
                string pluginPath = Path.Combine(assetsPath, "Plugins/");
                CreateFolders(PluginFolders, pluginPath); 

                // 创建项目根文件夹
                string projectPath = Path.Combine(assetsPath, ProjectName + "/");
                CreateFolders(ProjectFolders, projectPath); 

                //animator
                string aniamtorPath = Path.Combine(projectPath, "Animator/");
                CreateFolders(AnimatorFolders, aniamtorPath);

                //art
                string artPath = Path.Combine(projectPath, "Art/");
                CreateFolders(ArtFolders, artPath);

                //material
                string materialPath = Path.Combine(artPath, "Materials/");
                CreateFolders(MaterialFolders, materialPath);

                //mesh
                string meshPath = Path.Combine(artPath, "Meshes/"); ;
                CreateFolders(MeshFolders, meshPath);

                //texture
                string texturePath = Path.Combine(artPath, "Textures/"); ;
                CreateFolders(TextureFolders, texturePath);

                //ui
                string uiPath = Path.Combine(artPath, "UI/"); ;
                CreateFolders(UIFolders, uiPath);

                //code
                string codePath = Path.Combine(projectPath, "Code/"); ;
                CreateFolders(CodeFolders, codePath);

                //level
                string levelPath = Path.Combine(projectPath, "Level/"); ;
                CreateFolders(LevelFolders, levelPath);

                //media
                string mediaPath = Path.Combine(projectPath, "Media/"); ;
                CreateFolders(MediaFolders, mediaPath);

                //audio
                string audioPath = Path.Combine(mediaPath, "Audio/"); ;
                CreateFolders(AudioFolders, audioPath);
            }

            AssetDatabase.Refresh(); // 刷新资源数据库
        }

        /// <summary>
        /// 绘制编辑器窗口的 GUI
        /// </summary>
        protected void OnGUI()
        {
            // 显示帮助信息
            EditorGUILayout.HelpBox("Insert the project name used as the root folder.\nby HXu", MessageType.Info);
            GUILayout.Label("");

            // 输入项目名称
            ProjectName = EditorGUILayout.TextField("Project Name:", ProjectName);

            // 选择是否使用自定义文件夹结构
            UseMySelf = EditorGUILayout.Toggle("Use MySelf Structure", UseMySelf);

            this.Repaint(); // 刷新窗口

            GUILayout.Label("");
            //GUILayout.Space(70);

            // 生成按钮
            if (GUILayout.Button("Generate"))
            {
                CreateAllFolders(); // 创建所有文件夹
                this.Close(); // 关闭窗口
            }

            // 取消按钮
            if (GUILayout.Button("Cancel"))
            {
                this.Close(); // 关闭窗口
            }
        }
    }
}
#endif
